import Layout from '@/components/Layout.vue'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue';

// https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html#%E6%8D%95%E8%8E%B7%E6%89%80%E6%9C%89%E8%B7%AF%E7%94%B1%E6%88%96-404-not-found-%E8%B7%AF%E7%94%B1
export const NOT_FOUND_ROUTE = {
    name: 'NotFound',
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    meta: { isHidden: true },
};

const basicRoutes = [
    {
        path: '/',
        name: import.meta.env.VITE_APP_TITLE,
        component: Layout,
        meta: {
            icon: 'majesticons:compass-line',
        },
        children: [
            {
                path: '/',
                name: 'home',
                component: HomeView,
                meta: {
                    title: '主页',
                    icon: 'mdi:home',
                    order: 0,
                }
            },
            {
                path: '/about',
                name: 'about',
                component: AboutView,
                meta: {
                    title: '关于',
                    icon: 'mdi:about',
                    order: 1,
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/OAuthLoginView.vue'),
        meta: {
            title: '登录页',
            isHidden: true,
        },
    },
    {
        name: 'oauth',
        path: '/oauth',
        component: () => import('@/views/OAuthView.vue'),
        meta: {
            title: 'OAuth',
            isHidden: true,
        }
    },
    {
        name: '404',
        path: '/404',
        component: () => import('@/views/NotFound.vue'),
        meta: {
            title: '404',
            isHidden: true,
        }
    },
    {
        name: 'forbidden',
        path: '/forbidden',
        component: () => import('@/views/Forbidden.vue'),
        meta: {
            title: 'Forbidden',
            isHidden: true,
        }
    },
];

// routes 文件夹下的路由都会作为动态路由
const routes = import.meta.globEager('./routes/*.js');

const asyncRoutes = [];

Object.keys(routes).forEach((key) => {
    asyncRoutes.push(...routes[key].default)
});

export function allRoutes() {
    return [...basicRoutes, ...asyncRoutes];
}

function addAysncRoutes(router) {
    asyncRoutes.forEach(v => !router.hasRoute(v.name) && router.addRoute(v));
    router.addRoute(NOT_FOUND_ROUTE);
}

export { asyncRoutes, basicRoutes, addAysncRoutes }